<?php

	require_once 'mysqlconnect.php';
	
	function findSheetByUser($username){//根据用户名寻找订单
		global $dbc;
		$sheets = array ();
		$username = escape_data($username);
	
		$query = "SELECT * FROM shopping_sheet WHERE userid='".$username."'";
		$result = mysqli_query ( $dbc, $query );
		$row = mysqli_fetch_array ( $result, MYSQL_NUM );
		while ( $row ) {
			$oneSheet = array ();
			$oneSheet [0] = $row [0];
			$oneSheet [1] = $row [1];
			$oneSheet [2] = $row [2];
			$oneSheet [3] = $row [3];
			$oneSheet [4] = $row [4];
			$oneSheet [5] = $row [5];
			$oneSheet [6] = $row [6];
			$oneSheet [7] = $row [7];
			$oneSheet [8] = $row [8];
			$oneSheet [9] = $row [9];
			$oneSheet [10] = $row [10];
			$oneSheet [11] = $row [11];
		
			$sheets [] = $oneSheet;
			$row = mysqli_fetch_array ( $result, MYSQL_NUM );
		}
		return $sheets;
	}
	
	function findSheetByState($state){//根据订单状态寻找订单
		global $dbc;
		$sheets = array ();
		$state = escape_data($state);
	
		$query = "SELECT * FROM shopping_sheet WHERE sheetstate='".$state."'";
		$result = mysqli_query ( $dbc, $query );
		$row = mysqli_fetch_array ( $result, MYSQL_NUM );
		while ( $row ) {
			$oneSheet = array ();
			$oneSheet [0] = $row [0];
			$oneSheet [1] = $row [1];
			$oneSheet [2] = $row [2];
			$oneSheet [3] = $row [3];
			$oneSheet [4] = $row [4];
			$oneSheet [5] = $row [5];
			$oneSheet [6] = $row [6];
			$oneSheet [7] = $row [7];
			$oneSheet [8] = $row [8];
			$oneSheet [9] = $row [9];
			$oneSheet [10] = $row [10];
			$oneSheet [11] = $row [11];
		
			$sheets [] = $oneSheet;
			$row = mysqli_fetch_array ( $result, MYSQL_NUM );
		}
		return $sheets;
	}
	
	function deleteSheet($sheetid){//删除订单
		global $dbc;
		$sheetid = escape_data($sheetid);
		$ok = TRUE;
		
		$query1 = "DELETE * FROM shopping_sheet WHERE sheetid='".$sheetid."'";
		$result1 = mysqli_query ( $dbc, $query1 );
		$query2 = "DELETE * FROM sheet_content WHERE sheetid='".$sheetid."'";
		$result2 = mysqli_query ( $dbc, $query2 );
		
		if(!($result1 && $result2)){
			$ok = FALSE;
		}
		return $ok;
	}
	
	function chageState($sheetid, $state){//修改订单状态
		global $dbc;
		$sheetid = escape_data($sheetid);
		$state = escape_data($state);
		$ok = TRUE;
		
		$query1 = "UPDATE shopping_sheet SET sheetstate='".$state."' WHERE sheetid='".$sheetid."'";
		$result1 = mysqli_query ( $dbc, $query1 );
		
		if(!$result1){
			$ok = FALSE;
		}
		return $ok;
	}

?>